package com.irdstudio.apicenter.gateway.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.core.jdbc.MaccsBaseDAO;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;
import com.irdstudio.apicenter.gateway.web.MaccsException;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;
import com.irdstudio.apicenter.gateway.web.view.JsonView;
import com.irdstudio.apicenter.gateway.web.view.View;

/**
 * 获取应用营业网点数据
 * 
 * @author zs.ligm
 * @version 1.0
 * @date 2015-09-14
 */
@Controller
public class AppSaleSiteList implements IController {

	private static final String querySaleSiteSql = "select * from app_sale_site where app_id=? and sale_site_type=?";
	String sale_site_type = "SS";

	private static final double EARTH_RADIUS = 6378137;// 转换成单位M

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

	@Override
	public View execute(MaccsTradeDefineVo tradeDefine, MaccsPacket inPacket,
			HttpServletRequest request, HttpServletResponse response)
			throws MaccsException {

		// 接收移动客户端传过来的参数
		String appId = inPacket.getValue("appId");

		// 接收网点/商户类型,默认为网点
		sale_site_type = request.getParameter("sale_site_type");

		// 接收经度与纬度参数,如果未传,则取所有
		double nowLatitude = Double.parseDouble(request
				.getParameter("nowLatitude"));// 接收纬度信息
		double nowLongitude = Double.parseDouble(request
				.getParameter("nowLongitude"));// 接收经度信息

		// 获取APP用户数据权限
		MaccsBaseDAO jdbcDao = BeanUtils.getBean(MaccsBaseDAO.class);
		List<Map<String, Object>> saleSites = jdbcDao.findMapList(
				querySaleSiteSql, new Object[] { appId, sale_site_type });

		if (saleSites.size() < 1) {
			return new JsonView(request, response,
					MaccsPacketUtil.makeFailurePacket("无可访问营业网点数据!"));
		} else {
			// 查到商户/网点信息,筛选距离显示
			for (int i = 0; i < saleSites.size(); i++) {
				// 计算返回距离数
				double meter = getGreatCircleDistance(
						nowLongitude,
						nowLatitude,
						Double.parseDouble(saleSites.get(i)
								.get("sale_site_longitude").toString()),
						Double.parseDouble(saleSites.get(i)
								.get("sale_site_latitude").toString()));
				if (meter <= 5000.00) {
					saleSites.get(i).put("meter", meter);
				} else {
					saleSites.remove(saleSites.get(i));
				}
			}
			MaccsPacket outPacket = MaccsPacketUtil
					.makeSimpleSuccessPacket("OK!");
			outPacket.addVariable("menus", saleSites);
			return new JsonView(request, response, outPacket);
		}
	}

	/**
	 * 计算两个经纬度之间的距离
	 * 
	 * @param lng1
	 * @param lat1
	 * @param lng2
	 * @param lat2
	 * @return
	 */
	private double getGreatCircleDistance(double lng1, double lat1,
			double lng2, double lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}

}
